<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>导入Excel数据 - 测试用例管理</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/bootstrap-icons.css">
    <style>
        body {
            background-color: #f0f7ff;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding-top: 20px;
        }
        .navbar {
            background-color: #0d6efd;
            margin-bottom: 30px;
        }
        .container {
            max-width: 700px;
        }
        .card {
            border: none;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        .card-header {
            background-color: #0d6efd;
            color: white;
            font-weight: bold;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .alert {
            margin-top: 20px;
        }
        .instructions {
            background-color: #e7f1ff;
            padding: 15px;
            border-radius: 5px;
            margin-top: 20px;
        }
        .instructions h5 {
            color: #0d6efd;
            margin-top: 0;
            margin-bottom: 10px;
        }
        .instructions ul {
            margin-bottom: 0;
        }
        .btn-upload {
            position: relative;
            overflow: hidden;
        }
        .btn-upload input[type="file"] {
            position: absolute;
            top: 0;
            right: 0;
            min-width: 100%;
            min-height: 100%;
            font-size: 100px;
            opacity: 0;
            cursor: pointer;
        }
        .system-suggestions {
            position: absolute;
            z-index: 100;
            width: calc(100% - 1.5rem);
            max-height: 200px;
            overflow-y: auto;
            background-color: white;
            border: 1px solid #ced4da;
            border-radius: 0.25rem;
            display: none;
        }
        .system-suggestion-item {
            padding: 8px 12px;
            cursor: pointer;
        }
        .system-suggestion-item:hover {
            background-color: #e7f1ff;
        }
        .required-mark {
            color: #dc3545;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    {% include 'common/header.html' %}
    <!-- 主要内容 -->
    <div class="container">
        <div class="card">
            <div class="card-header">
                <h4><i class="bi bi-file-earmark-excel me-2"></i>导入Excel数据</h4>
            </div>
            <div class="card-body">
                <form method="POST" enctype="multipart/form-data" id="importForm">
                    <!-- 系统名称输入（必填） -->
                    <div class="form-group position-relative">
                        <label for="system_name" class="form-label">系统名称 <span class="required-mark">*</span></label>
                        <input type="text" class="form-control" id="system_name" name="system_name" required
                               placeholder="请输入系统名称">
                        <!-- 系统名称建议 -->
                        <div class="system-suggestions" id="systemSuggestions">
                            {% for system in system_list %}
                            <div class="system-suggestion-item" onclick="selectSystem('{{ system }}')">{{ system }}</div>
                            {% endfor %}
                        </div>
                        <div class="form-text">{{ field_descriptions['system_name'] }}，所有导入的用例将归属到此系统</div>
                    </div>
                    <!-- 新增版本号输入框 -->
                    <div class="form-group">
                        <label for="version" class="form-label">版本号 <span class="required-mark">*</span></label>
                        <input type="text" class="form-control" id="version" name="version" required
                               placeholder="例如：V1.0.0、V2.3.1等">
                        <div class="help-text">
                            导入的所有用例将使用此版本号<br>
                            系统将通过 版本号+用例编号+操作序列 确保唯一性
                        </div>
                    </div>
                    <!-- 文件上传 -->
                    <div class="form-group">
                        <label for="file" class="form-label">选择Excel文件 <span class="required-mark">*</span></label>
                        <div class="input-group">
                            <input type="file" class="form-control" id="file" name="file" accept=".xlsx, .xls" required
                                style="display: none;">
                            <button class="btn btn-outline-primary btn-upload" type="button" onclick="document.getElementById('file').click()">
                                <i class="bi bi-search me-1"></i>浏览文件
                            </button>
                            <span class="form-control" id="fileNameDisplay">未选择文件</span>
                        </div>
                        <small class="form-text text-muted">支持 .xlsx 和 .xls 格式的Excel文件，最大16MB</small>
                    </div>

                    <!-- 操作按钮 -->
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-upload me-1"></i>导入数据
                        </button>
                        <button type="button" class="btn btn-secondary ms-2" onclick="window.history.back()">
                            <i class="bi bi-arrow-left me-1"></i>取消
                        </button>
                    </div>
                </form>

                <!-- 导入结果提示 -->
                {% if request.args.get('message') %}
                <div class="alert alert-success">
                    {{ request.args.get('message').replace('\n', '<br>')|safe }}
                </div>
                {% endif %}

                {% if request.args.get('error') %}
                <div class="alert alert-danger">
                    {{ request.args.get('error') }}
                </div>
                {% endif %}
                <!-- 导入说明 -->
                <div class="instructions">
                    <h5><i class="bi bi-info-circle me-1"></i>导入说明</h5>
                    <ul>
                        <li>请先输入系统名称，所有导入的用例将归属到此系统</li>
                        <li>系统会自动处理Excel文件中的所有工作表（Sheet）</li>
                        <li>只有包含"用例编号"和"标题"列的工作表才会被导入</li>
                        <li>系统会自动跳过用例编号已存在的记录，避免重复导入</li>
                        <li>导入时会记录每条数据的来源工作表名称</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <!-- 字段说明模态框 -->
    {% include 'common/field_modal.html' %}

    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/system_suggest.js"></script>
    <script src="/static/js/util.js"></script>
</body>
</html>
